package com.example.article.controller;

import com.example.article.entity.ArticleEntity;
import com.example.article.service.ArticleService;
import com.example.commonutils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/save")
    public ResultData saveArticle(@RequestBody ArticleEntity article) {
        return articleService.saveArticle(article);
    }

    /**
     * 分页获取文章
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/articleInfoForPages/{page}/{limit}/{sort}")
    public ResultData articleInfoForPages(@PathVariable("page") Integer page,
                                          @PathVariable("limit") Integer limit,
                                          @PathVariable("sort") String sort, HttpServletRequest request) {
        return articleService.getArticleCommentInfo(page, limit, sort, request);
    }

    @GetMapping("/articleInfo/{articleId}")
    public ResultData getArticleInfo(@PathVariable("articleId") String articleId){
        return articleService.getArticleById(articleId);
    }

    @DeleteMapping("/deleteArticle/{articleId}")
    public ResultData deleteArticle(@PathVariable("articleId") String articleId,HttpServletRequest request){
        return articleService.deleteArticleById(articleId,request);
    }
}
